package com.gdqy.FCS.web;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.gdqy.FCS.common.api.FastResponse;
import com.gdqy.FCS.common.api.Fileupload;
import com.gdqy.FCS.common.api.ListResponse;
import com.gdqy.FCS.common.mvc.Args;
import com.gdqy.FCS.entity.Brand;
import com.gdqy.FCS.entity.Clothing;
import com.gdqy.FCS.entity.ClothingType;
import com.gdqy.FCS.entity.Comment;
import com.gdqy.FCS.entity.Design;
import com.gdqy.FCS.entity.Dictionary;
import com.gdqy.FCS.entity.EnterpriseInfo;
import com.gdqy.FCS.entity.Fabric;
import com.gdqy.FCS.entity.FabricComponent;
import com.gdqy.FCS.entity.User;
import com.gdqy.FCS.entity.Weave;
import com.gdqy.FCS.service.BrandService;
import com.gdqy.FCS.service.ClothingService;
import com.gdqy.FCS.service.ClothingTypeService;
import com.gdqy.FCS.service.CommentService;
import com.gdqy.FCS.service.DesignService;
import com.gdqy.FCS.service.DictionaryService;
import com.gdqy.FCS.service.EnterpriseInfoService;
import com.gdqy.FCS.service.FabricComponentService;
import com.gdqy.FCS.service.FabricService;
import com.gdqy.FCS.service.UserService;
import com.gdqy.FCS.service.WeaveService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("clothing")
public class ClothingController {
	@Autowired
	private ClothingService clothingService;
	@Autowired
	private ClothingTypeService clothingTypeService;
	@Autowired
	private BrandService brandService;
	@Autowired
	private EnterpriseInfoService enterpriseInfoService;
	@Autowired
	private FabricService fabricService;
	@Autowired
	private DesignService designService;
	@Autowired
	private CommentService commentService;
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private FabricComponentService fabricComponentService;
	@Autowired
	private WeaveService weaveService;
	@Autowired
	private UserService userService;

	// 处理增加服装请求
	@RequestMapping("deal_addClothing")
	public ModelAndView deal_addClothing(
			@RequestParam(value = "enterprise_id", required = true) int enterprise_id) {
		ModelAndView modelAndView = new ModelAndView();
		// 获取服装分类-大类
		List<ClothingType> clothingTypes = clothingTypeService
				.selectClothing_class("");
		modelAndView.addObject("clothingTypes", clothingTypes);
		// 获取字典中的款式
		List<Dictionary> dic_pattern = dictionaryService
				.selectByType_code("ZD005");
		modelAndView.addObject("dic_pattern", dic_pattern);
		// 字典风格
		List<Dictionary> dic_style = dictionaryService
				.selectByType_code("ZD004");
		modelAndView.addObject("dic_style", dic_style);
		// 品牌
		List<Brand> brands = brandService.selectByEnterprise_id(enterprise_id);
		modelAndView.addObject("brands", brands);

		// 面料成分 大类
		List<FabricComponent> fabricComponents = fabricComponentService
				.selectClass("");
		modelAndView.addObject("fabricComponents", fabricComponents);
		// 面料织法
		List<Weave> weaves = weaveService.selectByParent_code("");
		modelAndView.addObject("weaves", weaves);

		modelAndView.setViewName("enterprise/addClothes");
		return modelAndView;

	}

	// 增加服装
	@RequestMapping("addClothing")
	public ModelAndView addClothing(Clothing clothing,
			MultipartFile photo_pic1, MultipartFile photo_pic2,
			MultipartFile photo_pic3, MultipartFile photo_pic4,
			MultipartFile photo_pic5) throws IllegalStateException, IOException {
		String msg = "";
		ModelAndView modelAndView = new ModelAndView();
		String clothing_name = clothing.getName();
		if (clothing_name == "" || clothing_name == null) {
			msg = "上传失败";
			modelAndView.addObject("msg", msg);
			modelAndView.setViewName("enterprise/addDesign");
			return modelAndView;
		}
		EnterpriseInfo enterpriseInfo = enterpriseInfoService
				.selectById(clothing.getEnterprise_id());
		User user = userService.selectById(enterpriseInfo.getUser_id());
		String role = user.getRole();
		String username = user.getUsername();
		String type = "clothings";
		Fileupload fileupload = new Fileupload();
		String photo_url1 = fileupload.upload(photo_pic1, role, username, type);
		String photo_url2 = fileupload.upload(photo_pic2, role, username, type);
		String photo_url3 = fileupload.upload(photo_pic3, role, username, type);
		String photo_url4 = fileupload.upload(photo_pic4, role, username, type);
		String photo_url5 = fileupload.upload(photo_pic5, role, username, type);

		clothing.setPhoto_url1(photo_url1);
		clothing.setPhoto_url2(photo_url2);
		clothing.setPhoto_url3(photo_url3);
		clothing.setPhoto_url4(photo_url4);
		clothing.setPhoto_url5(photo_url5);

		// 设置被喜欢次数
		clothing.setLiked(0);
		// 设置被收藏次数
		clothing.setCollected(0);
		// 设置发布时间
		clothing.setRelease_time(new Date());
		// 设置修改时间
		clothing.setUpdate_time(new Date());
		// 设置状态（1=正常、0=删除）
		clothing.setStatus(1);
		int data = clothingService.insert(clothing);

		if (data != 0) {
			msg = "上传成功";
		} else {
			msg = "上传失败";
		}
		modelAndView.addObject("msg", msg);
		modelAndView.setViewName("enterprise/addClothes");
		return modelAndView;

	}

	// 列表浏览品牌服装
	@RequestMapping("deal_scan_clothing")
	public ModelAndView deal_scan_clothing(Args args, int pageindex,
			int pagesize) {
		ModelAndView modelAndView = new ModelAndView();
		// 获取服装--分页
		PageHelper.startPage(pageindex, pagesize);
		List<Clothing> clothings = clothingService.selectByLiked();
		PageInfo<Clothing> page = new PageInfo<Clothing>(clothings);
		ListResponse<Clothing> json = FastResponse._().errCode(0).subCode(0)
				.errMsg("获取列表成功").args(args.getReqeustargs())
				.pageIndex(page.getPageNum()).pageSize(page.getSize())
				.total((int) page.getTotal()).pageCount(page.getPages())
				.hasNext(page.isHasNextPage()).list(clothings);
		modelAndView.addObject("json", json);
		// 获取字典服装分类-大类
		List<ClothingType> clothing_big_class = clothingTypeService
				.selectClothing_class("");
		modelAndView.addObject("clothing_big_class", clothing_big_class);
		// 获取品牌
		List<Brand> brands = brandService.selectAll();
		modelAndView.addObject("brands", brands);
		// 风格
		List<Dictionary> style = dictionaryService.selectByType_code("ZD004");
		modelAndView.addObject("style", style);
		modelAndView.setViewName("enterprise/brand_clothes_list");
		return modelAndView;

	}

	// 显示服装详细
	@RequestMapping("clothing_detail")
	public ModelAndView clothing_detail(int clothing_id) {
		ModelAndView modelAndView = new ModelAndView();
		// 服装信息
		Clothing clothing = clothingService.selectById(clothing_id);
		modelAndView.addObject("clothing", clothing);
		// 企业信息
		int enterprise_id = clothing.getEnterprise_id();
		EnterpriseInfo enterpriseInfo = enterpriseInfoService
				.selectById(enterprise_id);
		modelAndView.addObject("enterpriseInfo", enterpriseInfo);
		// 分类
		ClothingType clothingType = clothingTypeService.selectByCode(clothing
				.getType_code());
		modelAndView.addObject("clothingType", clothingType);
		// 风格
		Dictionary style = dictionaryService.selectByCode(clothing
				.getStyle_code());
		modelAndView.addObject("style", style);
		// 款式
		Dictionary pattern = dictionaryService.selectByCode(clothing
				.getPattern_code());
		modelAndView.addObject("pattern", pattern);
		// 相似服装
		String type_code = clothing.getType_code();
		String style_code = clothing.getStyle_code();
		String pattern_code = clothing.getPattern_code();
		String component_code = clothing.getComponent_code();
		String weave_code = clothing.getWeave_code();
		String color = clothing.getColor();
		String season = clothing.getSeason();

		List<Clothing> similar_clothings = clothingService
				.selectSimilarClothing(type_code, style_code, pattern_code,
						component_code, weave_code, color, season, clothing_id);
		modelAndView.addObject("similar_clothings", similar_clothings);
		// 匹配面料
		List<Fabric> fabrics = fabricService
				.selectByComponent_code(component_code);
		modelAndView.addObject("fabrics", fabrics);
		// 匹配的设计作品
		List<Design> designs = designService.selectMatchClothingDesign(
				type_code, pattern_code, style_code);
		modelAndView.addObject("designs", designs);
		// 评论
		List<Comment> comments = commentService.selectByObj_idAndType(
				clothing_id, "2");
		modelAndView.addObject("comments", comments);
		modelAndView.setViewName("enterprise/clothes_detial");
		return modelAndView;
	}

	// 删除服装
	@RequestMapping("delete_clothing")
	public @ResponseBody String delete_clothing(
			@RequestParam(value = "clothing_id", required = true) int clothing_id,
			HttpSession session) {
		String msg = "fail";
		// 判断服装是否为该用户所有
		Clothing clothing = clothingService.selectById(clothing_id);
		int enterprise_id = (int) session.getAttribute("enterprise_id");
		if (clothing.getEnterprise_id() == enterprise_id) {
			int data = clothingService.deleteById(clothing_id);
			if (data > 0) {
				msg = "success";
			}
		}
		return msg;
	}

	// 通过服装大类、中类、小类、品牌 获取服装
	/**
	 * 
	 * 作者：张伟键 方法描述：通过服装大类、中类、小类、品牌 获取服装
	 * 
	 * @param big_code
	 *            服装分类大类
	 * @param middle_code
	 *            服装分类中类
	 * @param small_code服装分类小类
	 * @param brand_id
	 *            服装品牌
	 * @return
	 */
	@RequestMapping("getClothingByTypeAndBrand")
	public ModelAndView getClothingByTypeAndBrand(String big_code,
			String middle_code, String small_code, int brand_id) {
		ModelAndView modelAndView = new ModelAndView();
		List<Clothing> clothings = clothingService
				.selectClothingByTypeAndBrand(big_code, middle_code,
						small_code, brand_id);
		modelAndView.addObject("clothings", clothings);
		return modelAndView;
	}

	/************************************************************** Ajax请求 *****************************************************************************/
	@RequestMapping("getClothing_class")
	public @ResponseBody List<ClothingType> getClothing_class(
			@RequestParam(value = "parent_code", required = true) String parent_code) {
		List<ClothingType> clothingTypes = clothingTypeService
				.selectClothing_class(parent_code);
		return clothingTypes;
	}

	// 获取面料小类

	@RequestMapping("getFabricCompoent_class")
	public @ResponseBody List<FabricComponent> getFabricCompoent_class(
			String parent_code) {
		List<FabricComponent> fabricComponents = fabricComponentService
				.selectClass(parent_code);
		return fabricComponents;
	}

	// 获取织法小类
	@RequestMapping("getWeave_class")
	public @ResponseBody List<Weave> getWeave_class(String parent_code) {
		List<Weave> weaves = weaveService.selectByParent_code(parent_code);
		return weaves;
	}

	// 修改处
	// 获取服装列表
	@RequestMapping("getClothes")
	public ModelAndView selectClothingByClothingTypeAndBrand(String code,
			String brand_id, String style_code, int pageindex,int pagesize,Args args) {
		ModelAndView modelAndView = new ModelAndView();
		// 获取服装--分页
		PageHelper.startPage(pageindex, pagesize);
		List<Clothing> clothings = clothingService.selectClothingByClothingTypeAndBrand(code, brand_id, style_code);
		PageInfo<Clothing> page = new PageInfo<Clothing>(clothings);
		ListResponse<Clothing> json = FastResponse._().errCode(0).subCode(0)
				.errMsg("获取列表成功").args(args.getReqeustargs())
				.pageIndex(page.getPageNum()).pageSize(page.getSize())
				.total((int) page.getTotal()).pageCount(page.getPages())
				.hasNext(page.isHasNextPage()).list(clothings);
		modelAndView.addObject("json", json);
		System.out.print(json.getList().isEmpty());
		modelAndView.setViewName("enterprise/iframe_brand_clothes");
		return modelAndView;
	}
}
